import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const storePersist = (store) => {
  const key = 'VUEX PP OOO';
  const storage = window.sessionStorage;
  //取数
  const data = storage.getItem(key);
  data && store.replaceState(JSON.parse(data))
  //存储
  store.subscribe((mutation, store) => {
    storage.setItem(key, JSON.stringify(store))
  })
}

export default new Vuex.Store({
  state: {
    /**
     * 用户信息
     */
    user: {},
    /**
     * 激活的菜单
     */
    activeMenuId: 0
  },
  getters: {
    /**
     * token
     */
    token(state) {
      return state.user.token;
    },
    /**
     * 菜单
     */
    tree(state) {
      return state.user.tree;
    },
  },
  mutations: {
    /**
     * 设置用户信息
     */
    setUser(state, payload) {
      state.user = payload
    },
    /**
     * 设置激活的菜单Id
     */
    setActiveMenuId(state, payload) {
      state.activeMenuId = payload
    }
  },
  actions: {
    // /**
    //  * 退出
    //  */
    // exit({commit}){
    //   commit("setUser",{})
    //   commit("setMenus",{})
    //   router.push({name:"Login"})
    // }
  },
  modules: {
  },
  plugins: [storePersist]
})
